// const roleList = [
//   {
//     id: 1,
//     name: "超级管理员",
//     description: "超级管理员"
//   },
//   {
//     id: 2,
//     name: "管理员",
//     description: "管理员"
//   },
//   {
//     id: 3,
//     name: "普通用户",
//     description: "普通用户"
//   }
// ];

import { defineStore } from "pinia";
import { computed, reactive } from "vue";

export const useMockStore = defineStore(
  "mock",
  () => {
    const state = reactive({
      roleList: []
    });

    type TStateKey = typeof state;

    const roleList = computed(() => state.roleList);

    const addToList = <T extends keyof TStateKey>(
      data: unknown,
      collectionName: T
    ) => {
      if (Array.isArray(state[collectionName])) {
        (state[collectionName] as unknown[]).push(data);
      }
    };

    const updateToList = <T extends keyof TStateKey>(
      data: unknown,
      index: number,
      collectionName: T
    ) => {
      if (Array.isArray(state[collectionName])) {
        if (data) {
          (state[collectionName] as unknown[]).splice(index, 1, data);
        } else {
          (state[collectionName] as unknown[]).splice(index, 1);
        }
      }
    };

    return {
      state,
      roleList,
      addToList,
      updateToList
    };
  },
  {
    persist: {
      storage: window.localStorage,
      pick: ["state.roleList"]
    }
  }
);
